import TableConfig from "@/models/config/TableConfig";
import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
/**
 * 扩展表格
 * @param count 
 * @param dataArray 
 * @param table 
 */
export const extendCols = (count: number, dataArray: number[][], table: TableConfig) => {
    table.ColCount += count;
    for (var i = 0; i < table.RowCount; i++) {
        for (var j = 0; j < count; j++) {
            dataArray[i].push(-1);
        }
    }
};

/**
 * 更新表格
 * @param 结果 
 * @param 表格数组 
 * @param 表格配置 
 * @returns 
 */
export const updateTable = (result: EnumBjlResult, dataArray: number[][], table: TableConfig) => {
    //0:庄；1：闲
    if (
        table.X == 0 &&
        table.Y == 0 &&
        dataArray[table.X][table.Y] === -1
    ) {
        dataArray[table.X][table.Y] = result;
        return;
    }
    if (dataArray[table.X][table.Y] === result) {
        //连胜
        table.X++;
        if (
            table.X >= table.RowCount ||
            dataArray[table.X][table.Y] !== -1
        ) {
            table.X--;
            table.Y++;
            if (table.Y + 15 >= table.ColCount) {
                extendCols(5, dataArray, table);
            }
        }
        dataArray[table.X][table.Y] = result;
    } else {
        //连负
        table.X = 0;
        while (dataArray[table.X][table.Y] === -1) {
            table.Y--;
        }
        table.Y++;
        if (table.Y + 15 >= table.ColCount) {
            extendCols(10, dataArray, table);
        }
        dataArray[table.X][table.Y] = result;
    }
}

export const changeTable = (dataArray: number[][], table: TableConfig) => {
    table.Y++;
    //table.changeCount++;
    let flag = true;
    while (flag) {
        flag = false;
        for (let i = 0; i < table.RowCount; i++) {
            if (dataArray[i][table.Y] !== -1) {
                table.Y++;
                flag = true;
                break;
            }
        }
    }
    table.X = 0;
    let v = 999;
    dataArray[table.X][table.Y] = v;
    if (table.Y + 15 >= table.ColCount) {
        extendCols(5, dataArray, table);
    }
}
/**
 * 
 * @param td td元素
 */
export const scrollLeft = (td: HTMLElement) => {
    let scrollWidth = td.offsetLeft - 15 * 13;
    if (scrollWidth < 0) scrollWidth = 0;
    if (td.parentElement && td.parentElement.parentElement && td.parentElement.parentElement.parentElement) {
        td.parentElement.parentElement.parentElement.scrollLeft = scrollWidth;
    }
}